import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import view.produto.ProdutoCadastro;

import model.produto.Produto;

import com.towel.el.annotation.AnnotationResolver;
import com.towel.swing.table.ObjectTableModel;


public class ObjectTableModelDemo {
    public void show() {
	//Here we create the resolver for annotated classes.
        AnnotationResolver resolver = new AnnotationResolver(Produto.class);
	//We use the resolver as parameter to the ObjectTableModel
	//and the String represent the cols.
        final ObjectTableModel<Produto> tableModel = new ObjectTableModel<Produto>(
                resolver, "codigo,descricao");
	//Here we use the list to be the data of the table.
        tableModel.setData(getData());

        final JTable table = new JTable(tableModel);
        table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent evento) {
				if (evento.getClickCount() == 2)
					JOptionPane.showMessageDialog(null, tableModel.getValue(table.getSelectedRow()));
				
			}
		});
        
        JFrame frame = new JFrame("ObjectTableModel");
        JScrollPane pane = new JScrollPane();
        pane.setViewportView(table);
        pane.setPreferredSize(new Dimension(400,200));
        frame.add(pane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    //Just for create a default List to show.
    private List<Produto> getData() {
        List<Produto> list = new ArrayList<Produto>();
        list.add(new Produto(1l, "12345", "Caneta"));
        list.add(new Produto(2l, "11111", "Lapis"));
        list.add(new Produto(3l, "22222", "Borracha"));
        list.add(new Produto(4l, "33333", "Livro"));
        return list;
    }

    public static void main(String[] args) {
        new ObjectTableModelDemo().show();
    }
} 